
import 'dart:io';

import 'package:eq/native/constants.dart';
/// 命令行参数解析工具
String _eqInfo = '''
A command-line utility for EQ development.

Usage: eq <command|eq-file> [arguments]

Global options:
help                 Print this usage information.
version              Print the EQ SDK version.
enable-analytics     Enable analytics.
disable-analytics    Disable analytics.

Available commands:
  analyze    Analyze EQ code in a directory.
  format     Idiomatically format EQ source code.
  run        Run a EQ program.

Run "eq help <command>" for more information about a command.
See https://4mychip.com for detailed documentation.
''';

String _version = "0.0.2-stable";

String _analyzeInfo = '''
Analyze EQ code in a directory.

Usage: eq analyze [arguments] [<directory>]
help                   Print this usage information.

Run "eq help" to see global options.
''';
String _formatInfo = '''
Idiomatically format EQ source code.

Usage: eq format [options...] <files or directories...>
-h, --help                     Print this usage information.
-v, --verbose                  Show all options and flags with --help.
-o, --output                   Set where to write formatted output.

Run "eq help" to see global options.''';
String _runInfo = '''
Run a EQ program.

Usage: eq run [arguments] [<eq-file|package-target> [args]]
-h, --help                                             Print this usage information.
Run "eq help" to see global options.
''';

enum CMDGo { noOperate, filePath, analyzeCode, runCode, formatCode }

CMDGo analyzeCMD(List arg) {
  if (arg.isEmpty) {
    print(_eqInfo);
    exit(0);
  }
  switch (arg[0]) {
    case "help":
      if (arg.length == 1) {
        print(_eqInfo);
      } else {
        switch (arg[1]) {
          case "analyze":
            print(_analyzeInfo);
            break;
          case "format":
            print(_formatInfo);
            break;
          case "run":
            print(_runInfo);
            break;
          default:
            print(_eqInfo);
        }
      }
      break;
    case "version":
      print("EQ SDK version: $_version");
      break;
    case "format":
      return CMDGo.formatCode;
    case "run":
      return CMDGo.runCode;
    default:
      //不是命令行则是文件路径
      if ((arg[0] as String).endsWith(file_end)) {
        return CMDGo.filePath;
      } else {
        print("Could not find a command named:${arg[0]}");
        print(_eqInfo);
        exit(1);
      }
  }

  return CMDGo.noOperate;
}
